
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maBV = ISS.SESSION.getSession('session_maBV');

    var post= {};
    post.maBV = _maBV;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editPost',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(post),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllPostCategory',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllPostCategoryToView(responseContent);
        }
    });

}

function loadDataComplete(post){
    ISS.DOM.setVal('#AdPost_Code', post.maBV);
    ISS.DOM.setVal('#AdPost_Title', post.tieuDe);
    ISS.DOM.setVal('#AdPost_Summary', post.tomTat);
    ISS.DOM.setVal('#AdPost_Date', post.ngayViet);
    ISS.DOM.setVal('#AdPost_Content', post.noiDung);
    ISS.DOM.setVal('#AdPost_Category_hidden', post.danhMucBaiViet.maDMBV);
//    ISS.DOM.setVal('#AdPost_Picture', post.hinhAnh); // Cap nhat lai hinh anh
}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#AdPost_Edit', 'click', function () {
        sendInfoEdit();
    });
}

function sendInfoEdit() {
    var post = {};
    var category = {};

    post.maBV = ISS.DOM.getVal('#AdPost_Code');
    post.tieuDe = ISS.DOM.getVal('#AdPost_Title');
    post.tomTat = ISS.DOM.getVal('#AdPost_Summary');
    post.ngayViet = ISS.DOM.getVal('#AdPost_Date');
    post.noiDung = ISS.DOM.getVal('#AdPost_Content');
    post.hinhAnh = ISS.DOM.getVal('#AdPost_Picture');
    category.maDMBV = ISS.DOM.getVal('#AdPost_Category');
    post.danhMucBaiViet = category;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerPostEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(post),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(color) {
    if(color) {
        ISS.WINDOW.location('/jsp/admin/ad-post-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function mapAllPostCategoryToView(category) {
    var code = ISS.DOM.getVal('#AdPost_Category_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<category.length; i++) {
        if (category[i].maDMBV == code){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + category[i].maDMBV + '" ' + n + '  >' + category[i].tenDM + '</option>';
    }
    ISS.DOM.html('#AdPost_Category', html);
}
